package com.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.dao.FoodsDAO;
import com.dao.TopicDAO;
import com.dao.UsersDAO;
import com.entity.Foods;
import com.entity.Topic;
import com.entity.Users;
import com.service.CosService;
import com.util.VeDate;

@Service("cosService")
public class CosServiceImpl implements CosService {
	@Autowired
	private FoodsDAO foodsDAO;
	@Autowired
	private UsersDAO usersDAO;
	@Autowired
	private TopicDAO topicDAO;
	private String userid = "";
	private String[] foods = null;
	// 相似用户集合
	private List<List<Object>> similarityUsers = null;
	// 推荐所有食品集合
	private List<String> targetRecommendFoods = null;
	// 购买并评价过食品集合
	private List<String> commentedFoods = null;

	@Override
	public List<Foods> getRecommend(String userid, String cateid) {

		return null;
	}

	/**
	 * 把推荐列表中用户已经浏览过的食品剔除
	 */
	private void dealFoods(int[][] sparseMatrix, int foodSize) {
		int[] user2hist = new int[foodSize];
		for (int i = 0; i < 10; i++) {
			for (int j = 0; j < foodSize; j++) {
				user2hist[j] = sparseMatrix[i][j];
			}
		}
		commentedFoods = new ArrayList<String>();
		for (int i = 0; i < user2hist.length; i++) {
			if (sparseMatrix[0][i] != 0) {
				commentedFoods.add(foods[i]);
			}
		}
	}

	/**
	 * 计算食品推荐度，排序
	 */
	private void countFoods(int[][] sparseMatrix, int foodSize) {
		this.targetRecommendFoods = new ArrayList<String>();
		List<List<Object>> recommendFoods = new ArrayList<List<Object>>();
		List<Object> recommendFood = null;
		double recommdRate = 0, sumRate = 0;
		for (int i = 0; i < foodSize; i++) {
			recommendFood = new ArrayList<Object>();
			recommendFood.add(i);
			recommdRate = sparseMatrix[Integer.parseInt(similarityUsers.get(0).get(0).toString())][i]
					* Double.parseDouble(similarityUsers.get(0).get(1).toString())
					+ sparseMatrix[Integer.parseInt(similarityUsers.get(1).get(0).toString())][i]
							* Double.parseDouble(similarityUsers.get(1).get(1).toString());
			recommendFood.add(recommdRate);
			recommendFoods.add(recommendFood);
			sumRate += recommdRate;
		}
		recommendFoods = compare(recommendFoods);
		// 大于平均推荐度的食品才有可能被推荐
		for (int i = 0; i < recommendFoods.size(); i++) {
			List<Object> item = recommendFoods.get(i);
			if (Double.parseDouble(item.get(1).toString()) > sumRate / foodSize) { // 大于平均推荐度的食品才有可能被推荐
				System.out.println("foods= = >" + foods[Integer.parseInt(item.get(0).toString())]);
				this.targetRecommendFoods.add(foods[Integer.parseInt(item.get(0).toString())]);
			}
		}
	}

	// 获取用户相似度集合
	private void countUsers(int[][] sparseMatrix, int foodSize) {
		int[] selfTopic = new int[foodSize];
		List<List<Object>> tmpList = new ArrayList<List<Object>>();
		for (int i = 0; i < 10; i++) {
			if (i == 0) {
				for (int j = 0; j < foodSize; j++) {
					selfTopic[j] = sparseMatrix[i][j];
				}
				continue;
			}
			List<Object> userSimilarity = new ArrayList<Object>();
			int[] otherTopic = new int[foodSize];
			for (int j = 0; j < foodSize; j++) {
				otherTopic[j] = sparseMatrix[i][j];
			}
			userSimilarity.add(i);
			userSimilarity.add(countCos(selfTopic, otherTopic, foodSize));
			tmpList.add(userSimilarity);
			this.similarityUsers = compare(tmpList);
		}
	}

	// 根据用户数据，利用余弦计算用户相似度
	private static double countCos(int[] selfTopic, int[] otherTopic, int foodSize) {
		double sum = 0;
		double tmp1 = 0;
		double tmp2 = 0;
		double tmp3 = 0;
		for (int i = 0; i < foodSize; i++) {
			tmp1 += selfTopic[i] * otherTopic[i];
			tmp2 += selfTopic[i] * selfTopic[i];
			tmp3 += otherTopic[i] * otherTopic[i];
		}
		if (Math.sqrt(tmp2) * Math.sqrt(tmp3) == 0) {
			sum = 0;
		} else {
			sum = VeDate.getDouble(tmp1 / (Math.sqrt(tmp2) * Math.sqrt(tmp3)));
		}
		return sum;
	}

	/**
	 * 集合排序
	 */
	private static List<List<Object>> compare(List<List<Object>> tmpList) {
		for (int i = 0; i < tmpList.size(); i++) {
			for (int j = 0; j < tmpList.size() - i; j++) {
				List<Object> t1 = tmpList.get(i);
				List<Object> t2 = tmpList.get(j);
				if (Double.parseDouble("" + t1.get(1)) > Double.parseDouble("" + t2.get(1))) {
					List<Object> tmp = new ArrayList<Object>();
					tmp = t1;
					tmpList.set(i, t2);
					tmpList.set(j, tmp);
				}
			}
		}
		return tmpList;
	}
}
